---
id: fastbinaryformatter
sidebar_position: 7
title: 高性能序列化
sidebar_label: 4.7 高性能序列化
---

## 一、说明
该序列化以二进制方式进行序列化，不要求类型相同，仅为[相似结构](https://www.yuque.com/eo2w71/rrqm/d104f4159d543b4ae28db195ef623cf7#Y0Cko)即可。支持**基础类型**、**自定义实体类**、**结构体**、**数据**、**字典**、**List**、以及自定义序列化。
[
](https://www.nuget.org/packages/RRQMCore/)
## 二、序列化
```csharp
 public static void FastBinarySerialize(ByteBlock stream, object obj)
 {
     FastBinaryFormatter bf = new FastBinaryFormatter();
     bf.Serialize(stream, obj);
 }
```
## 三、反序列化
```csharp
public object RRQMBinaryDeserialize(byte[] data, int offset, Type type)
{
    FastBinaryFormatter bf = new FastBinaryFormatter();
    return bf.Deserialize(data, offset, type);
}
```
## 四、快捷序列化
在静态类`SerializeConvert`中，提供了快捷的序列化方式，以供方便使用。
## ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25438888/1663812694768-3b2d4cb6-c589-4479-9f9b-222a9ab65991.png#clientId=u051b4acd-2df2-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=326&id=ud292820e&name=image.png&originHeight=326&originWidth=1152&originalType=binary&ratio=1&rotation=0&showTitle=false&size=345481&status=done&style=none&taskId=u77352d5a-8e74-4dd0-ac51-49017d791ed&title=&width=1152)
## 
## 五、性能对比

**待测试类**

```csharp
 [Serializable]
public class Student
{
    public int P1 { get; set; }
    public string P2 { get; set; }
    public long P3 { get; set; }
    public byte P4 { get; set; }
    public DateTime P5 { get; set; }
    public double P6 { get; set; }
    public byte[] P7 { get; set; }

    public List<int> List1 { get; set; }
    public List<string> List2 { get; set; }
    public List<byte[]> List3 { get; set; }

    public Dictionary<int, int> Dic1 { get; set; }
    public Dictionary<int, string> Dic2 { get; set; }
    public Dictionary<string, string> Dic3 { get; set; }
    public Dictionary<int, Arg> Dic4 { get; set; }
}
[Serializable]
public class Arg
{
    public Arg(int myProperty)
    {
        this.MyProperty = myProperty;
    }

    public Arg()
    {
        Person person = new Person();
        person.Name = "张三";
        person.Age = 18;
    }

    public int MyProperty { get; set; }
}
[Serializable]
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
```

**测试代码**

```csharp
 Student student = new Student();
student.P1 = 10;
student.P2 = "若汝棋茗";
student.P3 = 100;
student.P4 = 0;
student.P5 = DateTime.Now;
student.P6 = 10;
student.P7 = new byte[1024 * 64];

Random random = new Random();
random.NextBytes(student.P7);

student.List1 = new List<int>();
student.List1.Add(1);
student.List1.Add(2);
student.List1.Add(3);

student.List2 = new List<string>();
student.List2.Add("1");
student.List2.Add("2");
student.List2.Add("3");

student.List3 = new List<byte[]>();
student.List3.Add(new byte[1024]);
student.List3.Add(new byte[1024]);
student.List3.Add(new byte[1024]);

student.Dic1 = new Dictionary<int, int>();
student.Dic1.Add(1, 1);
student.Dic1.Add(2, 2);
student.Dic1.Add(3, 3);

student.Dic2 = new Dictionary<int, string>();
student.Dic2.Add(1, "1");
student.Dic2.Add(2, "2");
student.Dic2.Add(3, "3");

student.Dic3 = new Dictionary<string, string>();
student.Dic3.Add("1", "1");
student.Dic3.Add("2", "2");
student.Dic3.Add("3", "3");

student.Dic4 = new Dictionary<int, Arg>();
student.Dic4.Add(1, new Arg(1));
student.Dic4.Add(2, new Arg(2));
student.Dic4.Add(3, new Arg(3));

BytePool bytePool = new BytePool(1024 * 1024 * 10, 102400);

TimeSpan timeSpan1 = TimeMeasurer.Run(() =>
{
    for (int i = 0; i < 100000; i++)
    {
        ByteBlock byteBlock = bytePool.GetByteBlock(1024 * 100);
        SerializeConvert.RRQMBinarySerialize(byteBlock, student,true);
        Student student1 = SerializeConvert.RRQMBinaryDeserialize<Student>(byteBlock.Buffer,
        byteBlock.Dispose();
        if (i % 1000 == 0)
        {
            Console.WriteLine(i);
        }
    }
});

TimeSpan timeSpan2 = TimeMeasurer.Run(() =>
{
    for (int i = 0; i < 100000; i++)
    {
        ByteBlock byteBlock = bytePool.GetByteBlock(1024 * 100);
        SerializeConvert.BinarySerialize(byteBlock, student);
        byteBlock.Position = 0;
        Student student1 = SerializeConvert.BinaryDeserialize<Student>(byteBlock);
        byteBlock.Dispose();
        if (i % 1000 == 0)
        {
            Console.WriteLine(i);
        }
    }
});

Console.WriteLine($"RRQM:{timeSpan1}");
Console.WriteLine($"System:{timeSpan2}");
```

**测试结果**

![](https://img-blog.csdnimg.cn/img_convert/787c262d6b61cdba0d8ee2f5a4239d8f.png#crop=0&crop=0&crop=1&crop=1&id=UIsJi&originHeight=1569&originWidth=2376&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
